/*
 * @Author: szx
 * @Date: 2022-07-12 12:15:36
 * @LastEditTime: 2022-07-12 12:35:45
 * @Description:
 * @FilePath: \leetcode\700-799\729\729.js
 */
var MyCalendar = function () {
    this.booked = [];
};

MyCalendar.prototype.book = function (start, end) {
    for (const arr of this.booked) {
        let l = arr[0],
            r = arr[1];
        if (l < end && start < r) {
            return false;
        }
    }
    this.booked.push([start, end]);
    return true;
};

class MyCalendar {
    constructor() {
        this.booked = [];
    }
    book(start, end) {
        const booked = this.booked;
        if (booked.length) {
            let left = 0;
            let right = booked.length;
            while (left < right) {
                const mid = Math.floor((left + right) / 2);
                const [s, e] = booked[mid];
                if (start >= e) {
                    left = mid + 1;
                } else if (end <= s) {
                    right = mid;
                } else {
                    return false;
                }
            }
            booked.splice(right, 0, [start, end]);
            return true;
        } else {
            booked.push([start, end]);
            return true;
        }
    }
}
